using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace BizTalkCrossReferenceManager.UI.Generation.Generators
{
    /// <summary>
    /// Generates the app type file
    /// </summary>
    public class BtsAppTypeGenerator
    {
        private const string FileName = "List_Of_App_Type.xml";
        private const string Start = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<listOfAppType xmlns=\"http://www.microsoft.com/uan\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.microsoft.com/uan ./common.xsd\">";
        private const string End = "</listOfAppType>";
    
        /// <summary>
        /// Generates the app type file
        /// </summary>
        /// <param name="outputFolderPath"></param>
        public static void Generate(string outputFolderPath)
        {
            string path = outputFolderPath + @"\" + FileName;
            
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(Start);

            CrossReferenceDataDataSet.xref_AppTypeDataTable table = new CrossReferenceDataDataSet.xref_AppTypeDataTable();
            CrossReferenceDataDataSetTableAdapters.xref_AppTypeTableAdapter adapter = new CrossReferenceDataDataSetTableAdapters.xref_AppTypeTableAdapter();
            adapter.Fill(table);

            foreach (CrossReferenceDataDataSet.xref_AppTypeRow row in table.Rows)
            {
                sb.AppendLine("\t<appType>");
                sb.AppendFormat("\t\t<name>{0}</name>", row.appType);
                sb.AppendLine();
                sb.AppendLine("\t\t<description/>");
                sb.AppendLine("\t</appType>");
            }

            sb.AppendLine(End);
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(fs))
                {
                    writer.Write(sb.ToString());
                    writer.Flush();
                }
            }
        }
    }
}
